package com.superbytecode.stencil.entities.table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.superbytecode.stencil.enums.SQLTypeEnum;
import com.superbytecode.stencil.utils.FieldUtil;
import com.superbytecode.stencil.utils.StringUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.structure.table
 * @ClassName: SQLTable
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/12 14:23
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Data
public class SQLTable implements Serializable {
    private static final long serialVersionUID = 6044091154602846303L;
    /**
     * 表名
     */
    private String tableName;
    /**
     * 对象名称
     */
    private String className;
    /**
     * 表注释
     */
    private String comments = "";
    /**
     * 字段
     */
    private List<JavaColumn> columns = Collections.emptyList();

    /*** 获取主键信息 **/
    @JsonIgnore
    public JavaColumn obtainColumnPrimaryKey() {
        // 过滤以"id"作为主键的column，或是否为主键的column
        return this.columns.stream().filter(Objects::nonNull)//
                .filter(c -> c.getIsPrimaryKey() || StringUtil.equalsIgnoreCase(c.getColumnName(), "id"))//
                .findAny() //
                .orElseThrow(() -> new RuntimeException(tableName + " primary column not found!"));
    }

    /*** java bean对象名称 **/
    public String obtainJavaBeanName(String prefixs) {
        return StringUtil.uppercaseFirstLetter(obtainJavaBeanNameLF(prefixs));
    }

    /*** java bean对象首字母小写，作为参数值使用 **/
    public String obtainJavaBeanNameLF(String prefixs) {
        // 删除指定前缀
        List<String> prefixList = StringUtil.isEmpty(prefixs) ? new ArrayList<>()//
                : Arrays.stream(prefixs.split(",")).filter(StringUtil::isNotBlank).collect(Collectors.toList());
        this.className = tableName;
        for (String prefix : prefixList) {
            className = className.startsWith(prefix) ? className.replace(prefix, "") : className;
        }
        className = FieldUtil.underscoreToCamelCase(className);
        className = StringUtil.dotFilter(className);
        return StringUtil.lowercaseFirstLetter(className);
    }

    public Long obtainSerialVersionUID() {
        return new Random().nextLong();
    }

    /*** 是否存在Decimal类型 **/
    public boolean bigDecimalExists() {
        return exists(SQLTypeEnum.DECIMAL);
    }

    /*** 是否存在List类型 **/
    public boolean listExists() {
        return false;
    }

    /*** 是否存在时间类型 **/
    public boolean dataTimeExists() {
        return exists(SQLTypeEnum.DATETIME);
    }

    private boolean exists(SQLTypeEnum settingType) {
        return this.columns.stream().filter(Objects::nonNull).anyMatch(c -> SQLTypeEnum.match(c.getColumnType(), settingType));
    }
}